using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace InstallDB
{
    [RunInstaller(true)]
    public partial class InstallerDBClass : Installer
    {
        public InstallerDBClass()
        {
            InitializeComponent();
        }


        private void ProcessBat()
        {
            RegistryKey regUser= Registry.CurrentUser;
            string srcRoot = (string)regUser.OpenSubKey("SOFTWARE").OpenSubKey
               ("Microsoft").OpenSubKey("Windows").OpenSubKey
               ("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("User Shell Folders").GetValue("Personal");
            srcRoot += "\\DbSetup\\";

            string oldDir = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(srcRoot);
            string batDir = /*dir + */"SetUpDB.bat";
            Process batProc = new Process();
            batProc.StartInfo.FileName = batDir;
            batProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //batProc.StartInfo.Arguments = dir;
            batProc.Start();
            batProc.WaitForExit();
            batProc.Close();
            
            Directory.SetCurrentDirectory(oldDir);
            //DeleteFile(dir);
        }

        private void ProcessWebconfig()
        {
            string dir = this.Context.Parameters["dir"];
            string filename = dir + "\\web.config";
            FileAttributes atts = File.GetAttributes(filename);
            atts = atts & (~FileAttributes.ReadOnly);
            File.SetAttributes(filename, atts);
            string content = File.ReadAllText(filename);
            content = content.Replace("localhost:2143/Crab.WebHost", "localhost/CrabHost");
            File.WriteAllText(filename, content);
        }

        private void ProcessAdam()
        {
            RegistryKey regUser = Registry.CurrentUser;
            string srcRoot = (string)regUser.OpenSubKey("SOFTWARE").OpenSubKey
               ("Microsoft").OpenSubKey("Windows").OpenSubKey
               ("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("User Shell Folders").GetValue("Personal");
            srcRoot += "\\Adam\\";

            string oldDir = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(srcRoot);
            string batDir = /*dir + */"setupadam.bat";
            Process batProc = new Process();
            batProc.StartInfo.FileName = batDir;
            batProc.StartInfo.CreateNoWindow = true;
            batProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //batProc.StartInfo.Arguments = dir;
            batProc.Start();
            batProc.WaitForExit();
            batProc.Close();

            Directory.SetCurrentDirectory(oldDir);
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            ProcessWebconfig();
            ProcessAdam();
            ProcessBat();
        }


		//----------------------------------------------------------------------------


		private void CleanUpDb()
		{
			RegistryKey regUser = Registry.CurrentUser;
			string srcRoot = (string)regUser.OpenSubKey("SOFTWARE").OpenSubKey
			   ("Microsoft").OpenSubKey("Windows").OpenSubKey
			   ("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("User Shell Folders").GetValue("Personal");
			srcRoot += "\\DbSetup\\";

			string oldDir = Directory.GetCurrentDirectory();

			Directory.SetCurrentDirectory(srcRoot);
			string batDir = /*dir + */"CleanUpDb.bat";
			Process batProc = new Process();
			batProc.StartInfo.FileName = batDir;
            batProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			//batProc.StartInfo.Arguments = dir;
			batProc.Start();
			batProc.WaitForExit();
			batProc.Close();
			Directory.SetCurrentDirectory(oldDir);
		}

		private void CleanUpAdam()
		{
			RegistryKey regUser = Registry.CurrentUser;
			string srcRoot = (string)regUser.OpenSubKey("SOFTWARE").OpenSubKey
			   ("Microsoft").OpenSubKey("Windows").OpenSubKey
			   ("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("User Shell Folders").GetValue("Personal");
			srcRoot += "\\Adam\\";

			string oldDir = Directory.GetCurrentDirectory();

			Directory.SetCurrentDirectory(srcRoot);
			string batDir = /*dir + */"cleanupadam.bat";
			Process batProc = new Process();
			batProc.StartInfo.FileName = batDir;
            batProc.StartInfo.CreateNoWindow = true;
            batProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
			//batProc.StartInfo.Arguments = dir;
			batProc.Start();
			batProc.WaitForExit();
			batProc.Close();
			Directory.SetCurrentDirectory(oldDir);
		}

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
			CleanUpAdam();
			CleanUpDb();
            base.Uninstall(savedState);

        }

    }
}